﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using LdtJudge.DatabaseHandle;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 档次与主刑关系表
    /// </summary>
    public class XF_Grade_RelationDAL
    {
        private string DbName;
        public XF_Grade_RelationDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(XF_Grade_RelationtModel model)
        {
            serverhelper sqlserverhelper = new serverhelper("");
            string sql=string.Format(@"insert into XF_Grade_Relation(
			GR_ID
          ,PG_ID
          ,GR_TypeID
          ,GR_Minimum
          ,GR_Supreme
          ,GR_Penalunit)
			values ('{0}','{1}','{2}','{3}','{4}','{5}')", model.GR_ID,model.PG_ID,model.GR_TypeID,model.GR_Minimum,model.GR_Supreme,model.GR_Penalunit);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ccId)
        {
            string sql = string.Format(@" delete from XF_Grade_Relation where GR_ID='{0}' ", ccId);           
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取档次与主刑关系表数据
        /// </summary>
        /// <param name="ccid">节点信息</param>
        /// <returns></returns>
        public DataTable GetPunishmentGrade(string ccid)
        {
            string sql = string.Format(@" SELECT *  FROM XF_Grade_Relation where GR_ID='{0}' ", ccid);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 修改，通过GR_ID
        /// </summary>
        public bool UpdateName(XF_Grade_RelationtModel model)
        {
            string sql = string.Format(@" update XF_Grade_Relation set PG_ID='{0}',GR_TypeID='{1}',GR_Minimum='{2}',GR_Supreme='{3}',GR_Penalunit='{4}' where GR_ID='{5}' ", model.PG_ID,model.GR_TypeID,model.GR_Minimum,model.GR_Supreme,model.GR_Penalunit,model.GR_ID);            
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        

    }
}
